package com.aoxiang.zbox.service.impl;

import com.aoxiang.zbox.mapper.LabelMapper;
import com.aoxiang.zbox.model.dto.LabelDTO;
import com.aoxiang.zbox.model.entity.Label;
import com.aoxiang.zbox.model.vo.SimpleLabelVO;
import com.aoxiang.zbox.service.LabelService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Description:
 *
 * @author: aoxiang
 * @create: 2025-03-08 14:26
 **/

@Service
public class LabelServiceImpl extends ServiceImpl<LabelMapper, Label> implements LabelService {
    @Autowired
    private LabelMapper labelMapper;

    @Override
    public void addCompletedSort(Long labelId) {
        LambdaUpdateWrapper<Label> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Label::getId, labelId);
        updateWrapper.set(Label::getCompletedSort, 1);
        labelMapper.update(null, updateWrapper);
    }

    @Override
    public boolean isCompleted(Long labelId) {
        LambdaQueryWrapper<Label> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Label::getId, labelId);
        queryWrapper.isNotNull(Label::getCompletedSort);
        return labelMapper.exists(queryWrapper);
    }

    @Override
    public void updateCompletedSort(List<Long> ids) {
        if (ids.size() == 0) {
            return;
        }
        LambdaUpdateWrapper<Label> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.isNotNull(Label::getCompletedSort);
        updateWrapper.set(Label::getCompletedSort, Integer.MAX_VALUE);
        labelMapper.update(null, updateWrapper);
        for (int i = 1; i <= ids.size(); i++) {
            labelMapper.updateCompletedSort(ids.get(i - 1), i);
        }
    }

    @Override
    public void updateShowSort(List<Long> ids) {
        if (ids.size() == 0) {
            return;
        }
        LambdaUpdateWrapper<Label> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.isNotNull(Label::getShowSort);
        updateWrapper.set(Label::getShowSort, null);
        labelMapper.update(null, updateWrapper);
        for (int i = 1; i <= ids.size(); i++) {
            labelMapper.updateShowSort(ids.get(i - 1), i);
        }
    }

    @Override
    public void addShow(Long id) {
        LambdaQueryWrapper<Label> updateWrapper = new LambdaQueryWrapper<>();
        updateWrapper.isNotNull(Label::getShowSort);
        updateWrapper.select(Label::getId, Label::getShowSort);
        updateWrapper.orderByAsc(Label::getShowSort);
        List<Label> labels = labelMapper.selectList(updateWrapper);
        Integer currentMaxShowSort;
        if (labels.size() == 0) {
            currentMaxShowSort = 0;
        } else {
            currentMaxShowSort = labels.get(labels.size() - 1).getShowSort();
        }
        Map<Long, Integer> showSoartMap = labels.stream().collect(Collectors.toMap(Label::getId, Label::getShowSort));
        if (showSoartMap.containsKey(id)) {
            Integer sort = showSoartMap.get(id);
            // sort后面的全部加一
            showSoartMap.forEach((k, v) -> {
                if (v >= sort) {
                    labelMapper.updateShowSort(k, v + 1);
                }
            });
        } else {
            labelMapper.updateShowSort(id, currentMaxShowSort + 1);
        }
    }

    @Override
    public List<SimpleLabelVO> getShownSimple() {
        return labelMapper.getShownSimple();
    }

    @Override
    public List<SimpleLabelVO> getAllSimple() {
        return labelMapper.getAllSimple();
    }

    @Override
    public void updateById(LabelDTO labelDto) {
        LambdaUpdateWrapper<Label> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Label::getId, labelDto.getId());
        updateWrapper.set(Label::getName, labelDto.getName());
        labelMapper.update(null, updateWrapper);
    }

    @Override
    public Long createLabel(LabelDTO labelDto) {
        LambdaQueryWrapper<Label> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Label::getName, labelDto.getName());
        Label label = labelMapper.selectOne(queryWrapper);
        if (label == null) {
            Label label1 = new Label();
            label1.setName(labelDto.getName());
            label1.setIsDeleted(0);
            label1.setColor(labelDto.getColor());
            labelMapper.insert(label1);
            return label1.getId();
        }else {
            LambdaUpdateWrapper<Label> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Label::getId, label.getId());
            updateWrapper.set(Label::getIsDeleted, 0);
            updateWrapper.set(Label::getColor, labelDto.getColor());
            labelMapper.update(null, updateWrapper);
            return label.getId();
        }
    }


}
